package com.jeesite.modules.task.action.web;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.idgen.IdGen;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.backlog.service.DcSupBacklogService;
import com.jeesite.modules.common.service.SysMsgService;
import com.jeesite.modules.common.service.SysOfficeUnitService;
import com.jeesite.modules.file.dao.FileUploadDao;
import com.jeesite.modules.file.entity.FileUpload;
import com.jeesite.modules.file.utils.FileUploadUtils;
import com.jeesite.modules.home.entity.DcSupTaskActionExplain;
import com.jeesite.modules.home.service.DcSupTaskActionExplainService;
import com.jeesite.modules.sys.entity.Role;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.annotation.RepeatSubmit;
import com.jeesite.modules.home.entity.DcSupTaskActionExplain;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.task.action.entity.DcSupTaskAction;
import com.jeesite.modules.task.action.entity.DcSupTaskActionDto;
import com.jeesite.modules.task.action.entity.DcSupTaskInfoAction;
import com.jeesite.modules.task.action.service.DcSupTaskActionService;
import com.jeesite.modules.task.deploy.entity.DcSupTaskInfo;
import com.jeesite.modules.task.deploy.entity.DcSupTaskInfoAdd;
import com.jeesite.modules.task.deploy.service.DcSupTaskInfoAddService;
import com.jeesite.modules.task.deploy.service.DcSupTaskInfoService;
import com.jeesite.modules.task.dict.entity.DcSupTaskInfoDict;
import com.jeesite.modules.task.dict.service.DcSupTaskInfoDictService;
import com.jeesite.modules.task.feedback.entity.DcSupTaskFeedback;
import com.jeesite.modules.task.receive.entity.TaskReceiveDto;
import com.jeesite.modules.task.taskgroup.entity.DcSupTaskGroup;
import com.jeesite.modules.task.taskgroup.service.DcSupTaskGroupService;
import com.jeesite.modules.utils.DateTimeUtil;
import com.jeesite.modules.utils.OfficeUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 任务部署分发表Controller
 * @author zrr
 * @version 2022-03-03
 */
@Controller
@RequestMapping(value = "${adminPath}/action/dcSupTaskAction")
public class DcSupTaskActionController extends BaseController {

	@Autowired
	private DcSupTaskActionService dcSupTaskActionService;
	@Autowired
	private DcSupTaskInfoService dcSupTaskInfoService;
	@Autowired
	private DcSupTaskInfoAddService dcSupTaskInfoAddService;
	@Autowired
	private DcSupTaskInfoDictService dcSupTaskInfoDictService;
	@Autowired
	private DcSupTaskActionExplainService dcSupTaskActionExplainService;
	@Autowired
	private DcSupTaskGroupService dcSupTaskGroupService;
	@Autowired
	private FileUploadDao fileUploadDao;
	@Autowired
	private SysOfficeUnitService sysOfficeUnitService;
	@Autowired
	private SysMsgService sysMsgService;
	@Autowired
	private DcSupBacklogService dcSupBacklogService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public DcSupTaskAction get(String id, boolean isNewRecord) {
		return dcSupTaskActionService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequestMapping(value = {"list", ""})
	public String list(DcSupTaskAction dcSupTaskAction, Model model) {
		model.addAttribute("dcSupTaskAction", dcSupTaskAction);
		return "modules/task/action/dcSupTaskActionList";
	}

	@RequestMapping(value = {"actionForm", ""})
	public String actionForm(DcSupTaskAction dcSupTaskAction, Model model) {
		model.addAttribute("actionForm", dcSupTaskAction);
		return "modules/task/delete/actionForm";
	}

	/**
	 * Tab1
	 * @param dcSupTaskAction
	 * @param model
	 * @return
	 */
	@RequiresPermissions("action:dcSupTaskAction:view")
	@RequestMapping(value = "noCompleteView")
	public String noCompleteView(DcSupTaskAction dcSupTaskAction, Model model) {
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		// 查询任务类型
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);
		model.addAttribute("dicts", dicts);
		model.addAttribute("dcSupTaskAction", dcSupTaskAction);
		return "modules/task/action/dcSupTaskActionNoCompleteList";
	}

	/**
	 * Tab2
	 * @param dcSupTaskAction
	 * @param model
	 * @return
	 */
	@RequiresPermissions("action:dcSupTaskAction:view")
	@RequestMapping(value = "completeListView")
	public String completeListView(DcSupTaskAction dcSupTaskAction, Model model) {
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		// 查询任务类型
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);
		model.addAttribute("dicts", dicts);
		model.addAttribute("dcSupTaskAction", dcSupTaskAction);
		return "modules/task/action/dcSupTaskActionCompleteList";
	}

	/**
	 * 秘书一科反馈任务详情页面
	 */
	@RequestMapping("feebackListView")
	public String ActionFeedbackListView(DcSupTaskInfo dcSupTaskInfo,Model model){
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		dcSupTaskInfo.setTaskYear(currentYear.toString());
		model.addAttribute("dicts", dicts);
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("dcSupTaskInfo",dcSupTaskInfo);
		return "modules/task/action/dcSupTaskActionFeedbackList";
	}

	/**
	 * 秘书一科反馈任务详情页面数据
	 */
	@ResponseBody
	@RequestMapping("feedbackList")
	public Page<DcSupTaskInfo> ActionFeedbackList(DcSupTaskInfo dcSupTaskInfo, HttpServletRequest request, HttpServletResponse response){
		dcSupTaskInfo.setPage(new Page<>(request,response));
		return dcSupTaskActionService.actionSecretaryFeedbackList(dcSupTaskInfo);
	}

	/**
	 * 导出任务数据
	 * @param dcSupTaskAction
	 * @param request
	 * @param response
	 */
	@PostMapping(value = "exportData")
	@ResponseBody
	public void exportData(DcSupTaskInfoAction dcSupTaskAction, HttpServletRequest request, HttpServletResponse response) throws Exception {
		dcSupTaskActionService.exportData(dcSupTaskAction, request, response);
	}

	/**
	 * 导出任务数据 word
	 * @param dcSupTaskInfo
	 * @param request
	 * @param response
	 */
	@PostMapping(value = "exportWord")
	@ResponseBody
	public void exportWord(DcSupTaskInfo dcSupTaskInfo, HttpServletRequest request, HttpServletResponse response) throws Exception {
		dcSupTaskActionService.exportWord(dcSupTaskInfo, request, response);
	}
	
	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<DcSupTaskAction> listData(DcSupTaskAction dcSupTaskAction, HttpServletRequest request, HttpServletResponse response) {
		dcSupTaskAction.setPage(new Page<>(request, response));
		List<String> times = DateTimeUtil.timeString(dcSupTaskAction.getTimeParam());
		if(times != null) {
			dcSupTaskAction.setStartTime(times.get(0));
			dcSupTaskAction.setEndTime(times.get(1));
		}
		//当前判断是综合查询的查看反馈
		if(dcSupTaskAction.getRemarks1() != null && dcSupTaskAction.getRemarks1()==1){
			String str =  Global.getConfig("comprehensive.query.permission", "0");
			List<Role> roleList = UserUtils.getUser().getRoleList();
			if(regRole(str,roleList)){
				dcSupTaskAction.setLeaderOrgCode(sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode());
			}
		}
		//任务待办-已办-查看反馈
		if(dcSupTaskAction.getRemarks1() != null && dcSupTaskAction.getRemarks1()==2){
			dcSupTaskAction.setLeaderOrgCode(sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode());
		}
		Page<DcSupTaskAction> page = dcSupTaskActionService.findPage(dcSupTaskAction);
		return page;
	}

	//判断登录人的角色是不是包含参数里的角色
	private boolean regRole(String str, List<Role> roleList) {
		List<String> list = Arrays.stream(str.split(",")).collect(Collectors.toList());
		for (int i = 0; i < list.size(); i++) {
			for (int j = 0; j < roleList.size(); j++) {
				if(StringUtils.equals(list.get(i),roleList.get(j).getRoleCode())){
					return false;
				}
			}
		}
		return true;
	}

	/**
	 * 任务删除子页面数据
	 * @param dcSupTaskAction
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "listDataDelete")
	@ResponseBody
	public Page<DcSupTaskAction> listDataDelete(DcSupTaskAction dcSupTaskAction, HttpServletRequest request, HttpServletResponse response) {
		dcSupTaskAction.setPage(new Page<>(request, response));
		List<String> times = DateTimeUtil.timeString(dcSupTaskAction.getTimeParam());
		if(times != null) {
			dcSupTaskAction.setStartTime(times.get(0));
			dcSupTaskAction.setEndTime(times.get(1));
		}
		Page<DcSupTaskAction> page = dcSupTaskActionService.listDataDelete(dcSupTaskAction);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("action:dcSupTaskAction:view")
	@RequestMapping(value = "form")
	public String form(DcSupTaskAction dcSupTaskAction, Model model) {
		model.addAttribute("dcSupTaskAction", dcSupTaskAction);
		return "modules/task/action/dcSupTaskActionForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("action:dcSupTaskAction:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated DcSupTaskAction dcSupTaskAction) {
		dcSupTaskActionService.save(dcSupTaskAction);
		return renderResult(Global.TRUE, text("保存任务部署分发信息成功！"));
	}
	
	/**
	 * 停用数据
	 */
	@RequiresPermissions("action:dcSupTaskAction:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(DcSupTaskAction dcSupTaskAction) {
		dcSupTaskAction.setStatus(DcSupTaskAction.STATUS_DISABLE);
		dcSupTaskActionService.updateStatus(dcSupTaskAction);
		return renderResult(Global.TRUE, text("停用任务部署分发信息成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequiresPermissions("action:dcSupTaskAction:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(DcSupTaskAction dcSupTaskAction) {
		dcSupTaskAction.setStatus(DcSupTaskAction.STATUS_NORMAL);
		dcSupTaskActionService.updateStatus(dcSupTaskAction);
		return renderResult(Global.TRUE, text("启用任务部署分发信息成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("action:dcSupTaskAction:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(DcSupTaskAction dcSupTaskAction) {
		dcSupTaskActionService.delete(dcSupTaskAction);
		return renderResult(Global.TRUE, text("删除任务部署分发信息成功！"));
	}

	/**
	 * 查询待办任务列表
	 */
	@RequestMapping("noCompleteListData")
	@RequiresPermissions("action:dcSupTaskAction:view")
	@ResponseBody
	public Page<DcSupTaskInfoAction> noCompleteListData(DcSupTaskInfoAction dcSupTaskAction, HttpServletRequest request, HttpServletResponse response){
		dcSupTaskAction.setPage(new Page<>(request, response));
		Page<DcSupTaskInfoAction> page = dcSupTaskActionService.noCompleteListData2(dcSupTaskAction);
		return page;
	}

	/**
	 * 查询已办任务列表
	 */
	@RequestMapping("completeListData")
	@RequiresPermissions("action:dcSupTaskAction:view")
	@ResponseBody
	public Page<DcSupTaskInfoAction> completeListData(DcSupTaskInfoAction dcSupTaskAction,HttpServletRequest request, HttpServletResponse response){
		dcSupTaskAction.setPage(new Page<>(request, response));
		return dcSupTaskActionService.completeListData(dcSupTaskAction);
	}


	/**
	 * 查询子任务列表
	 */
	@RequestMapping("listOrg")
	public String listOrg(DcSupTaskAction dcSupTaskAction, Model model) {
		// remarks1=1控制催办显示
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		model.addAttribute("dcSupTaskAction", dcSupTaskAction);
		return "modules/task/action/dcSupTaskActionList";
	}

	/**
	 * 用于任务删除页面,因为页面有删除按钮,所以新增页面
	 * @param dcSupTaskAction
	 * @param model
	 * @return
	 */
	@RequestMapping("listOrgDelete")
	public String listOrgDelete(DcSupTaskAction dcSupTaskAction, Model model) {
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		model.addAttribute("dcSupTaskAction", dcSupTaskAction);
		return "modules/task/delete/dcSupTaskActionListDelete";
	}

	/**
	 * 综合查询,引用任务部署点击任务标题页面,因为页面有返回按钮,所以新增页面
	 * @param dcSupTaskAction
	 * @param model
	 * @return
	 */
	@RequestMapping("listOrg1")
	public String listOrg1(DcSupTaskAction dcSupTaskAction, Model model) {
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		model.addAttribute("dcSupTaskAction", dcSupTaskAction);
		return "modules/task/action/dcSupTaskActionList1";
	}

	/**
	 * 查看反馈
	 * 单个牵头单位，就跳转到反馈列表；多个牵头单位，就跳转到牵头单位列表
	 * 反馈人、收文员，只能看到本单位的；督查单位工作人员，可以看到所有的
	 * @param dcSupTaskAction
	 * @param model
	 * @return
	 */
	@RequestMapping("showInfoFeedbackDetail")
	public String showInfoFeedbackDetail(DcSupTaskAction dcSupTaskAction, Model model) {
		// 通过传入的taskId查询其下有几个牵头责任单位
		DcSupTaskAction temp = new DcSupTaskAction();
		temp.setPage(new Page<>());
		temp.setTaskId(dcSupTaskAction.getTaskId());
		String str = Global.getConfig("comprehensive.query.permission", "0");
		List<Role> roleList = UserUtils.getUser().getRoleList();
		if (regRole(str, roleList)) {
			temp.setLeaderOrgCode(sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode());
		}
		Page<DcSupTaskAction> page = dcSupTaskActionService.findPage(temp);

		// 单个牵头单位，就跳转到反馈列表；多个牵头单位，就跳转到牵头单位列表
		List<DcSupTaskAction> actionList = page.getList();
		if (actionList != null && actionList.size() == 1) {
			TaskReceiveDto taskReceive = new TaskReceiveDto();
			taskReceive.setRemarks2("0");
			taskReceive.setIsNewRecord(false);
			taskReceive.setId(actionList.get(0).getId());
			taskReceive.setTaskId(dcSupTaskAction.getTaskId());
			model.addAttribute("taskReceiveFive", taskReceive);
			model.addAttribute("nowDate", new Date());
			return "modules/task/receive/taskReceiveFive";
		} else {
			Integer currentYear = Integer.parseInt(DateUtils.getYear());
			model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
			model.addAttribute("currentYear", currentYear);
			model.addAttribute("dcSupTaskAction", dcSupTaskAction);
			return "modules/task/action/dcSupTaskActionList1";
		}
	}

	/**
	 *
	 * 任务详情
	 *
	 *
	 */
	@RequestMapping(value = "dcSupTaskInfoProgress")
	public String dcSupTaskInfoProgress(DcSupTaskAction dcSupTaskAction, Model model) {
		//获取主表数据
		DcSupTaskInfo taskInfo = new DcSupTaskInfo();
		taskInfo.setId(dcSupTaskAction.getTaskId());
		taskInfo = dcSupTaskInfoService.get(taskInfo);
		//TODO 获取子表数据
		dcSupTaskAction = dcSupTaskActionService.get(dcSupTaskAction);

		//获取反馈表数据
		DcSupTaskFeedback dcSupTaskFeedback = new DcSupTaskFeedback();
		dcSupTaskFeedback.setTaskId(dcSupTaskAction.getTaskId());
		dcSupTaskFeedback.setActionId(dcSupTaskAction.getId());

		model.addAttribute("taskReceiveOne", dcSupTaskFeedback);
		//返回右边数据
		model.addAttribute("taskInfo", taskInfo);
		model.addAttribute("dcSupTaskAction", dcSupTaskAction);
		return "modules/task/deploy/dcSupTaskInfoProgress";
	}

	/**
	 * 待办批示查询列表页
	 */
	@RequiresPermissions("action:dcSupTaskAction:view")
	@RequestMapping(value = {"lists", ""})
	public String lists(DcSupTaskActionDto dcSupTaskActionDto, Model model) {
		//查询任务年度1
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);
		model.addAttribute("dicts", dicts);
		dcSupTaskActionDto.setTaskYear(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(null));
		model.addAttribute("dcSupTaskActionDto", dcSupTaskActionDto);
		return "modules/task/action/dcSupTaskActionListOne";
	}

	/**
	 * 待办批示列表数据
	 */
	@RequiresPermissions("action:dcSupTaskAction:view")
	@RequestMapping(value = "findListData")
	@ResponseBody
	public Page<DcSupTaskActionDto> findListData(DcSupTaskActionDto dcSupTaskActionDto, HttpServletRequest request, HttpServletResponse response) {
		dcSupTaskActionDto.setPage(new Page<>(request, response));
		Page<DcSupTaskActionDto> listData = dcSupTaskActionService.findListData(dcSupTaskActionDto, request, response);
		return listData;
	}

	/**
	 * 待办批示查看编辑表单
	 */
	@RequiresPermissions("action:dcSupTaskAction:view")
	@RequestMapping(value = "forms")
	public String forms(DcSupTaskActionDto dcSupTaskActionDto, Model model) {
		// 如果有批示，则将批示放入表单中，查看
		DcSupTaskInfo dcSupTaskInfo = dcSupTaskInfoService.get(dcSupTaskActionDto.getTaskId());
		DcSupTaskInfoAdd dcSupTaskInfoAdd = new DcSupTaskInfoAdd();
		dcSupTaskInfoAdd.setTaskId(dcSupTaskActionDto.getTaskId());
		dcSupTaskInfo.setDcSupTaskInfoAddList(dcSupTaskInfoAddService.findList(dcSupTaskInfoAdd));
		model.addAttribute("dcSupTaskInfo", dcSupTaskInfo);
		DcSupTaskAction dcSupTaskAction = dcSupTaskActionService.get(dcSupTaskActionDto.getId());
		dcSupTaskActionDto.setExplainFlag(dcSupTaskAction.getExplainFlag());
		dcSupTaskActionDto.setExplainContent(dcSupTaskAction.getExplainContent());
		dcSupTaskActionDto.setExplainTime(dcSupTaskAction.getExplainTime());
		dcSupTaskActionDto.setExplainName(dcSupTaskAction.getExplainName());
		model.addAttribute("dcSupTaskActionDto", dcSupTaskActionDto);
		DcSupTaskActionExplain explain = new DcSupTaskActionExplain();
		explain.setActionId(dcSupTaskActionDto.getTaskId());
		List<DcSupTaskActionExplain> explainList =  dcSupTaskActionExplainService.findList(explain);
		model.addAttribute("explainList", explainList);
		return "modules/task/action/dcSupTaskInfoDetailFormWithOne";
	}

	/**
	 * 待办批示保存数据
	 */
	@RequiresPermissions("action:dcSupTaskAction:edit")
	@PostMapping(value = "saveData")
	@ResponseBody
	public String updateData(@Validated DcSupTaskAction dcSupTaskAction) {
		// 用户批示任务时，将此处的单条消息置为已读
		sysMsgService.makeMsgReadByBiz(dcSupTaskAction.getId(), "renwupishi", UserUtils.getUser().getUserCode());
		dcSupBacklogService.doBacklog(dcSupTaskAction.getId(),"task_pishi");
		dcSupTaskAction.setExplainFlag("2");
		dcSupTaskActionService.updateData(dcSupTaskAction);
		return renderResult(Global.TRUE, text("领导批示成功！"));
	}

	/**
	 * 定时任务,每日修改action的task_progress
	 */
	@RequestMapping(value = "scheduledActionEveryday")
	@ResponseBody
	public void scheduledActionEveryday() {
		dcSupTaskActionService.scheduledActionEveryday();
	}

	/**
	 * 批量修改老数据的task_progress
	 */
	@RequestMapping(value = "changeActionTaskProgress")
	@ResponseBody
	public String changeActionTaskProgress() {
		return renderResult(Global.TRUE, text("修改全部action的task_progress"),dcSupTaskActionService.changeActionTaskProgress());
	}

	/**
	 * 定时任务-每日修改逾期未反馈状态到action的task_progress
	 */
	@RequestMapping(value = "scheduledActionTaskProgress")
	@ResponseBody
	public String scheduledActionTaskProgress() {
		return renderResult(Global.TRUE, text("每日修改action的task_progress"),dcSupTaskActionService.scheduledActionTaskProgress());
	}

	/**
	 * 每日更新info表的task_status
	 * action表的task_status
	 * 当feedback表中的feedback_status为5和9,设置action表中的task_status为已结束
	 * 当全部action表中的task_status为已结束时,设置info表中的task_status为已结束
	 */
	@RequestMapping(value = "scheduledInfoStatusEveryday")
	@ResponseBody
	public void scheduledInfoStatusEveryday() {
		dcSupTaskActionService.scheduledInfoStatusEveryday();
	}



	/**
	 * 去分解任务
	 */
	@RequestMapping(value = "decompose")
	public String decompose(DcSupTaskAction dcSupTaskAction, Model model) {
		DcSupTaskInfo	dcSupTaskInfo = null;
		if(StringUtils.isNotBlank(dcSupTaskAction.getId())){
				dcSupTaskInfo = dcSupTaskInfoService.get(dcSupTaskAction.getTaskId());
		}
		dcSupTaskInfo.setRemarks3(dcSupTaskAction.getId());
		DcSupTaskGroup dcSupTaskGroup = new DcSupTaskGroup();
		dcSupTaskGroup.setCreateBy(UserUtils.getUser().getUserCode());
		List<DcSupTaskGroup> groupList = dcSupTaskGroupService.findList(dcSupTaskGroup);
		model.addAttribute("groupList", groupList);
		dcSupTaskInfo.setTaskLeaderOrg("");
		dcSupTaskInfo.setTaskLeaderOrgName("");
		DcSupTaskInfoAdd dcSupTaskInfoAdd = new DcSupTaskInfoAdd();
		dcSupTaskInfoAdd.setTaskId(dcSupTaskInfo.getId());
		List<DcSupTaskInfoAdd> list = dcSupTaskInfoAddService.findList(dcSupTaskInfoAdd);
		if(StringUtils.equals(dcSupTaskInfo.getNormalFlag(),"3")){

			Map<String, List<DcSupTaskInfoAdd>> map = list.stream().collect(Collectors.groupingBy(DcSupTaskInfoAdd::getTaskRefineId));
			Set<String> strings = map.keySet();
			for(String s:strings){
				list = map.get(s);
				break;
			}

		}

		dcSupTaskInfo.setDcSupTaskInfoAddList(list);

		// 自动获取年度，近几年前台可自由配置
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);

		// 第一次打开form，默认为普通任务
		model.addAttribute("msgType", "dc_num_common");
		if (StringUtils.isBlank(dcSupTaskInfo.getNormalFlag())) {
			dcSupTaskInfo.setNormalFlag("1");
			model.addAttribute("msgType", "dc_num_common");
		} else if (StringUtils.equals("2", dcSupTaskInfo.getNormalFlag())) {
			// 秘书一科任务编辑时，回显督办科室
			model.addAttribute("msgType", "dc_num_secret");
		}

		// 为联系人和联系单位赋初值
		User currUser = UserUtils.getUser();
		dcSupTaskInfo.setContactName(StringUtils.isBlank(dcSupTaskInfo.getContactName()) ? currUser.getUserName() : dcSupTaskInfo.getContactName());
		dcSupTaskInfo.setContactPhone(StringUtils.isBlank(dcSupTaskInfo.getContactPhone()) ? currUser.getMobile() : dcSupTaskInfo.getContactPhone());


		// 初始化排序编号
		if (StringUtils.isBlank(dcSupTaskInfo.getSortNum())) {
			Integer maxSortNum = dcSupTaskInfoService.getMaxSortNum();
			if (maxSortNum == null) {
				maxSortNum = 1;
			} else {
				maxSortNum = maxSortNum + 10;
			}
			dcSupTaskInfo.setSortNum(dcSupTaskInfo.getSortNum() != null ? dcSupTaskInfo.getSortNum() : maxSortNum + "");
		}

		// 查询督办人列表
		// 查询任务类型列表
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);
		//--->1查询所有附件fileList
			List<FileUpload> fileUploadList = FileUploadUtils.findFileUpload(dcSupTaskInfo.getId(), "dcSupTaskInfo_file");

			//---->2复制所有附件类型为dcSupTaskAction_file
		String newId = IdGen.nextId();
		for (int j = 0; j < fileUploadList.size(); j++) {
				FileUpload fileUpload = fileUploadList.get(j);
				fileUpload.setBizType("dcSupTaskInfo_file");
				fileUpload.setBizKey(newId);
				fileUpload.setId(null);
				fileUploadDao.insert(fileUpload);
			}
		dcSupTaskInfo.setId(newId);
		if(StringUtils.equals("999",dcSupTaskInfo.getTaskTypeCode())){
		dcSupTaskInfo.setDecomposeFlag("1");}
		model.addAttribute("dicts", dicts);
		model.addAttribute("dcSupTaskInfo", dcSupTaskInfo);

		return "modules/task/action/dcSupTaskAcitonDeCompose";
	}


	@PostMapping(value = "saveDeCompose")
	@RepeatSubmit
	@ResponseBody
	public String saveDeCompose( DcSupTaskInfo dcSupTaskInfo) {
		try {
			// 获取当前会话对象
			dcSupTaskInfo.setCreateOrg(OfficeUtil.getCurrentOffice().getOfficeCode());
			dcSupTaskInfo.setCreateOrgName(OfficeUtil.getCurrentOffice().getOfficeName());
			dcSupTaskInfo.setDecomposeFlag("0");
			dcSupTaskInfo.setParentTaskId("0");
			dcSupTaskInfo.setSourceTaskId("0");
			dcSupTaskInfoService.saveDeCompose(dcSupTaskInfo);
			return renderResult(Global.TRUE, text("发布任务成功！"));
		} catch (Exception e) {
			e.printStackTrace();
			return renderResult(Global.FALSE, text(e.getMessage()));
		}
	}


	/**
	 * 查询列表
	 */
	@RequestMapping(value = "decomposeList")
	public String decomposeList(DcSupTaskInfo dcSupTaskInfo, Model model) {
		if(StringUtils.isNotBlank(dcSupTaskInfo.getId())){
			dcSupTaskInfo =dcSupTaskInfoService.get(dcSupTaskInfo);
		}

		model.addAttribute("dcSupTaskInfo", dcSupTaskInfo);
		// 自动获取年度，近几年前台可自由配置
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		// 查询牵头领导
		User user = new User();
		user.setRoleCode("100016");
		user.setUserType("employee");
		// 查询任务类型
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);

		model.addAttribute("dicts", dicts);
		return "modules/task/action/dcSupTaskAcitonDeComposeList";
	}

	@RequestMapping(value = "deleteAction")
	@ResponseBody
	public String deleteAction(DcSupTaskAction dcSupTaskAction) {
		dcSupTaskActionService.deleteAction(dcSupTaskAction);
		return renderResult(Global.TRUE, text("删除牵头单位成功！"));
	}

	@RequestMapping(value = "updateTaskStatus")
	@ResponseBody
	public String updateTaskStatus(DcSupTaskAction action){
		dcSupTaskActionService.updateTaskStatus(action);
		return renderResult(Global.TRUE, text("修改成功,请刷新页面!"));
	}

	@RequestMapping(value = "getActionById")
	@ResponseBody
	public String getActionById(DcSupTaskAction action){
		return renderResult(Global.TRUE, text("查询成功"),dcSupTaskActionService.getActionById(action));
	}
}